<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="mongo分片集群 + KeyFile 认证, 技术,教训,心情">
    <meta name="description" content="mongo 集群搭建踩坑">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="referrer" content="no-referrer-when-downgrade">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>mongo分片集群 + KeyFile 认证 | 600客街</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery-3.6.0.min.js"></script>

<meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/atom.xml" title="600客街" type="application/atom+xml">
</head>




<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">600客街</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/contact" class="waves-effect waves-light">
      
      <i class="fas fa-comments" style="zoom: 0.6;"></i>
      
      <span>留言板</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友情链接</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">600客街</div>
        <div class="logo-desc">
            
            学习！学习！学习！
            
        </div>
    </div>

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/contact" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-comments"></i>
			
			留言板
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友情链接
		</a>
          
        </li>
        
        
    </ul>
</div>


        </div>

        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/postimages/mongo-shard.png')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">mongo分片集群 + KeyFile 认证</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/mongo/">
                                <span class="chip bg-color">mongo</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/%E7%AC%94%E8%AE%B0/" class="post-category">
                                笔记
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2022-01-15
                </div>
                

                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    1.6k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    8 分
                </div>
                

                
            </div>
        </div>
        <hr class="clearfix">

        
        <!-- 是否加载使用自带的 prismjs. -->
        <link rel="stylesheet" href="/libs/prism/prism.css">
        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <p> <a target="_blank" rel="noopener" href="https://www.cnblogs.com/jxkshu/archive/2020/04/29/mongodb.html">原文地址</a></p>
<h2 id="一、环境准备"><a href="#一、环境准备" class="headerlink" title="一、环境准备"></a>一、环境准备</h2><blockquote>
<ol>
<li>服务器节点信息【三台服务器】<br>　　x.x.x.33、 x.x.x..37、 x.x.x..41</li>
<li>服务器系统<br>　　linux x86_64 GNU/Linux 或者 centos7 或者 redhat</li>
<li>mongodb版本<br>　　<a target="_blank" rel="noopener" href="https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.5.tgz">mongodb v4.2.5 下载地址</a></li>
</ol>
</blockquote>
<h2 id="二、集群环境设计"><a href="#二、集群环境设计" class="headerlink" title="二、集群环境设计"></a>二、集群环境设计</h2><blockquote>
<ol>
<li>三个 分片 复制集合<br>　　　　shard1(x.x.x.33:28017、 x.x.x.37:28017、 x.x.x.41:28017)<br>　　　　shard2(x.x.x.33:28018、 x.x.x.37:28018、 x.x.x.41:28018)<br>　　　　shard3(x.x.x.33:28019、 x.x.x.37:28019、 x.x.x.41:28019)</li>
<li>一个 config 复制集合<br>　　　　(x.x.x.33:27017、 x.x.x.37:27017、 x.x.x.41:27017)</li>
<li>一个 mongos 节点<br>　　　　(x.x.x.37:27000)</li>
</ol>
</blockquote>
<h2 id="三、mongo-安装"><a href="#三、mongo-安装" class="headerlink" title="三、mongo 安装"></a>三、mongo 安装</h2><blockquote>
<p>$ tar -zxvf mongodb-linux-x86_64-rhel70-4.2.5.tgz<br>$  mv mongodb-linux-x86_64-rhel70-4.2.5 /opt/mongodb4.2</p>
</blockquote>
<h2 id="四、添加环境变量"><a href="#四、添加环境变量" class="headerlink" title="四、添加环境变量"></a>四、添加环境变量</h2><blockquote>
<p>1.vim /etc/profile<br>2. 文件末尾追加:           export PATH=$PATH:/opt/mongodb/bin<br>3. 加载文件立刻生效:    source /etc/profile</p>
</blockquote>
<h2 id="五、搭建mongodb分片复制集"><a href="#五、搭建mongodb分片复制集" class="headerlink" title="五、搭建mongodb分片复制集"></a>五、搭建mongodb分片复制集</h2><p>分别在三台服务器(x.x.x.33、x.x.x.37、 x.x.x.41)节点 执行如下命令</p>
<h3 id="5-1-创建目录"><a href="#5-1-创建目录" class="headerlink" title="5.1 创建目录"></a>5.1 创建目录</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">创建分片数据存储目录 dbPath 存储目录
 <span class="token function">mkdir</span> -p /opt/mongodb4.2/data/db1  【分片1】
 <span class="token function">mkdir</span> -p /opt/mongodb4.2/data/db2  【分片2】
 <span class="token function">mkdir</span> -p /opt/mongodb4.2/data/db3  【分片3】

分片集群配置文件 目录
 <span class="token function">mkdir</span> -p /opt/mongodb4.2/conf

分片日志目录
 <span class="token function">mkdir</span> -p /opt/mongodb4.2/logs<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="5-2-创建集群安全认证机制KeyFile"><a href="#5-2-创建集群安全认证机制KeyFile" class="headerlink" title="5.2 创建集群安全认证机制KeyFile"></a>5.2 创建集群安全认证机制KeyFile</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">openssl rand -base64 <span class="token number">736</span> <span class="token operator">></span> /opt/mongodb4.2/conf/mongodb.keyfile<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p><code>注意</code>: keyfile 必须满足以下条件:</p>
<ol>
<li>至少6个字符，小于1024字节。</li>
<li>认证的时候不考虑文件中的空白字符。</li>
<li>连接副本集成员的KeyFile和启动mongos进程的 KeyFile文件内容必须一致。</li>
<li>三台服务器的 keyFile 必须一样</li>
<li>必须是base64编码，但不能有等号。</li>
<li>文件权限必须是 600(chmod 600),不能分配任何权限给group成员和其他成员。</li>
</ol>
<h3 id="5-3-编写分片复制集配置文件"><a href="#5-3-编写分片复制集配置文件" class="headerlink" title="5.3 编写分片复制集配置文件"></a>5.3 编写分片复制集配置文件</h3><p>x.x.x.37 为例,【分片1】 配置文件:<br><code>vim /opt/mongodb4.2/conf/mongodb1.conf</code><br>不同的 conf 修改对应注释的内容<br>mongodb1.conf  db1  mongodb1.log 28017  jxk1<br>mongodb2.conf  db2  mongodb2.log 28018  jxk2<br>mongodb2.conf  db3  mongodb3.log 28019  jxk3</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">storage:
  dbPath: /opt/mongodb4.2/data/db1 <span class="token comment"># 不同副本不同</span>
  journal:
      enabled: <span class="token boolean">true</span>
  wiredTiger:
      engineConfig:
          cacheSizeGB: <span class="token number">2</span>
          directoryForIndexes: <span class="token boolean">true</span>
systemLog:
  destination: <span class="token function">file</span>
  logAppend: <span class="token boolean">true</span>
  path: /opt/mongodb4.2/logs/mongodb1.log <span class="token comment"># 日志</span>
net:
  port: <span class="token number">27017</span> <span class="token comment"># 端口</span>
  bindIp: <span class="token number">127.0</span>.0.1,192.x.x.37 <span class="token comment"># 远程 0.0.0.0</span>
processManagement:
  fork: <span class="token boolean">true</span>
replication:
  oplogSizeMB: <span class="token number">4000</span>
  replSetName: jxk1  <span class="token comment"># 不同副本名称不同</span>
sharding:
  clusterRole: shardsvr
<span class="token comment"># 注意，如果需要建立用户，这部分先注释则不进行 auth 校验，等创建完用户再开启</span>
<span class="token comment">#security:</span>
<span class="token comment">#  authorization: enabled</span>
<span class="token comment">#  keyFile: /opt/mongodb4.2/conf/mongodb.keyfile</span>
<span class="token comment">#  clusterAuthMode: keyFile</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="5-4-启动-分片复制集"><a href="#5-4-启动-分片复制集" class="headerlink" title="5.4 启动 分片复制集"></a>5.4 启动 分片复制集</h3><blockquote>
<p>1.分别在三台服务器(x.x.x.33、x.x.x.37、 x.x.x.41)节点 执行如下命令<br>2. mongod -f /opt/mongodb4.2/conf/mongodb1.conf<br>3. mongod -f /opt/mongodb4.2/conf/mongodb2.conf<br>4. mongod -f /opt/mongodb4.2/conf/mongodb3.conf</p>
</blockquote>
<h3 id="5-5-初始化分片复制集"><a href="#5-5-初始化分片复制集" class="headerlink" title="5.5 初始化分片复制集"></a>5.5 初始化分片复制集</h3><p>　　1. 在其中任意一台节点服务器登录 本人使用 x.x.x.159节点<br>　　2. mongo -port 27017 【登录】<br>　　3. 进入mongodb命令输入终端 输入命令如下</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">var conf <span class="token operator">=</span> <span class="token punctuation">&#123;</span>
    _id:<span class="token string">'shard'</span>, <span class="token comment"># 注意名称和分片的副本名称一致</span>
    members:<span class="token punctuation">[</span>
        <span class="token punctuation">&#123;</span>_id:1, host:<span class="token string">'x.x.x.33:28017'</span><span class="token punctuation">&#125;</span>,
        <span class="token punctuation">&#123;</span>_id:2, host:<span class="token string">'x.x.x.37:28017'</span><span class="token punctuation">&#125;</span>,
        <span class="token punctuation">&#123;</span>_id:3, host:<span class="token string">'x.x.x.41:28017'</span><span class="token punctuation">&#125;</span>,
    <span class="token punctuation">]</span>,
<span class="token punctuation">&#125;</span>
rs.initiate<span class="token punctuation">(</span>conf<span class="token punctuation">)</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="5-6-设置分片复制集账号"><a href="#5-6-设置分片复制集账号" class="headerlink" title="5.6 设置分片复制集账号"></a>5.6 设置分片复制集账号</h3><p>1.查看集群服务器信息<br>rs.status()</p>
<p>2.找到mongodb自己选举出来的 主库（Master），其他库不行，输入命令<br> <pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">use admin
db.createUser<span class="token punctuation">(</span><span class="token punctuation">&#123;</span>
    user: <span class="token string">"username"</span>,
    <span class="token builtin class-name">pwd</span> <span class="token builtin class-name">:</span> <span class="token string">"password"</span>,
    roles:<span class="token punctuation">[</span>
        <span class="token punctuation">&#123;</span>role:<span class="token string">"root"</span>, db:<span class="token string">"admin"</span><span class="token punctuation">&#125;</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span>
db.auth<span class="token punctuation">(</span><span class="token string">"username"</span>, <span class="token string">"password"</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></p>
<h3 id="5-7-验证"><a href="#5-7-验证" class="headerlink" title="5.7 验证"></a>5.7 验证</h3><p>到从库验证主库建立的用户是否可以通过授权<br><code>db.auth(&quot;username&quot;, &quot;password&quot;)</code><br>注意：<br>    防火墙状态，防火墙端口是否开启</p>
<h2 id="六、搭建config节点复制集"><a href="#六、搭建config节点复制集" class="headerlink" title="六、搭建config节点复制集"></a>六、搭建config节点复制集</h2><p>分别在三台服务器(x.x.x.33、x.x.x.37、x.x.x.41)节点 执行如下命令</p>
<h3 id="6-1-创建目录"><a href="#6-1-创建目录" class="headerlink" title="6.1 创建目录"></a>6.1 创建目录</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">创建 config 数据存储 目录
<span class="token function">mkdir</span> -p /opt/mongodb4.2/cfg/data

创建 config 日志 目录
<span class="token function">mkdir</span> -p /opt/mongodb4.2/cfg/logs<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="6-2-创建复制集配置文件"><a href="#6-2-创建复制集配置文件" class="headerlink" title="6.2 创建复制集配置文件"></a>6.2 创建复制集配置文件</h3><p>vim /opt/mongodb4.2/conf/mongodb-cfg.conf</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">systemLog:
    destination: <span class="token function">file</span>
    logAppend: <span class="token boolean">true</span>
    path: /opt/mongodb4.2/cfg/logs/mongodb.log
storage:
    journal:
        enabled: <span class="token boolean">true</span>
    dbPath: /opt/mongodb4.2/cfg/data
    directoryPerDB: <span class="token boolean">true</span>
    wiredTiger:
        engineConfig:
            cacaeSizeGB: <span class="token number">2</span>
            directoryForIndexes: <span class="token boolean">true</span>
        collectionConfig:
            blockCompressor: zstd
        indexConfig:
            prefixCompression: <span class="token boolean">true</span>
net:
    port: <span class="token number">27017</span>
    bindIp: x.x.x.37
replication:
    oplogSizeMB: <span class="token number">4000</span>
    replSetName: configReplSet
sharding:
    clusterRole: configsvr
processManagement:
    fork: <span class="token boolean">true</span>　
<span class="token comment"># 注意，如果需要建立用户，这部分先注释则不进行 auth 校验，等创建完用户再开启</span>
<span class="token comment">#security:</span>
<span class="token comment">#  authorization: enabled</span>
<span class="token comment">#  keyFile: /opt/mongodb4.2/conf/mongodb.keyfile</span>
<span class="token comment">#  clusterAuthMode: keyFile        </span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="6-3-启动config节点复制集"><a href="#6-3-启动config节点复制集" class="headerlink" title="6.3 启动config节点复制集"></a>6.3 启动config节点复制集</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">mongod -f /opt/mongodb4.2/conf/mongodb-cfg.conf<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<h3 id="6-4-登录config节点"><a href="#6-4-登录config节点" class="headerlink" title="6.4 登录config节点"></a>6.4 登录config节点</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">mongo -host x.x.x.<span class="token punctuation">(</span><span class="token number">33</span>/37/41<span class="token punctuation">)</span> -port <span class="token number">27017</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<h3 id="6-5-初始化-config-节点"><a href="#6-5-初始化-config-节点" class="headerlink" title="6.5 初始化 config 节点"></a>6.5 初始化 config 节点</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">var conf <span class="token operator">=</span> <span class="token punctuation">&#123;</span>
    _id:<span class="token string">'configReplSet'</span>,
    members:<span class="token punctuation">[</span>
        <span class="token punctuation">&#123;</span>_id:0, host:<span class="token string">"x.x.x.37:27017"</span><span class="token punctuation">&#125;</span>,
        <span class="token punctuation">&#123;</span>_id:1, host:<span class="token string">"x.x.x.33:27017"</span><span class="token punctuation">&#125;</span>,
        <span class="token punctuation">&#123;</span>_id:2, host:<span class="token string">"x.x.x.41:27017"</span><span class="token punctuation">&#125;</span>,
    <span class="token punctuation">]</span>
<span class="token punctuation">&#125;</span>
rs.initiate<span class="token punctuation">(</span>conf<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="6-6-创建认证用户"><a href="#6-6-创建认证用户" class="headerlink" title="6.6 创建认证用户"></a>6.6 创建认证用户</h3><p>选择<code>主库</code>,并进入</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">use admin
db.createUser<span class="token punctuation">(</span><span class="token punctuation">&#123;</span>
    user: <span class="token string">"username"</span>,
    <span class="token builtin class-name">pwd</span> <span class="token builtin class-name">:</span> <span class="token string">"password"</span>,
    roles:<span class="token punctuation">[</span>
        <span class="token punctuation">&#123;</span>role:<span class="token string">"root"</span>, db:<span class="token string">"admin"</span><span class="token punctuation">&#125;</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span>
db.auth<span class="token punctuation">(</span><span class="token string">"username"</span>, <span class="token string">"password"</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="七-搭建-mongos-节点"><a href="#七-搭建-mongos-节点" class="headerlink" title="七 搭建 mongos 节点"></a>七 搭建 mongos 节点</h2><p>在 x.x.x.37 服务器上搭建客户端</p>
<h3 id="7-1-创建mongos-日志目录"><a href="#7-1-创建mongos-日志目录" class="headerlink" title="7.1 创建mongos 日志目录"></a>7.1 创建mongos 日志目录</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">mkdir</span> -p /opt/mongodb4.2/mongos/logs<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<h3 id="7-2-创建mongos-配置文件"><a href="#7-2-创建mongos-配置文件" class="headerlink" title="7.2 创建mongos 配置文件"></a>7.2 创建mongos 配置文件</h3><p>vim /opt/mongodb4.2/conf/mongos.conf</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">systemLog:
    destination: <span class="token function">file</span>
    logAppend: <span class="token boolean">true</span>
    path: /opt/mongodb4.2/mongos/logs/mongodb.log
net:
    port: <span class="token number">27000</span>
    bindIp: <span class="token number">192</span>.x.x.37
sharding:
    configDB: configReplSet/x.x.x.33:27017,x.x.x.37:27017,x.x.x.41:27017
processManagement:
    fork: <span class="token boolean">true</span> 
security:
    keyFile: /opt/mongodb4.2/conf/mongodb.keyfile
    clusterAuthMode: keyFile<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="7-3-启动mongos"><a href="#7-3-启动mongos" class="headerlink" title="7.3 启动mongos"></a>7.3 启动mongos</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">mongos -f /opt/mongodb4.2/conf/mongos.conf<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<h3 id="7-4-登录并初始化-mongos"><a href="#7-4-登录并初始化-mongos" class="headerlink" title="7.4 登录并初始化 mongos"></a>7.4 登录并初始化 mongos</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">mongo -port <span class="token number">27000</span>
use admin
db.auth<span class="token punctuation">(</span><span class="token string">"username"</span>, <span class="token string">"password"</span><span class="token punctuation">)</span>  <span class="token comment">#这里使用的账号是 config复制集的账号</span>
db.runCommand<span class="token punctuation">(</span><span class="token punctuation">&#123;</span>
    addshard:<span class="token string">"jxk1/x.x.x.33:28017,x.x.x.37:28017,x.x.x.41:28017,"</span>,
    name: <span class="token string">"shard1"</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
db.runCommand<span class="token punctuation">(</span><span class="token punctuation">&#123;</span>
    addshard:<span class="token string">"jxk2/x.x.x.33:28018,x.x.x.37:28018,x.x.x.41:28018,"</span>,
    name: <span class="token string">"shard2"</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
db.runCommand<span class="token punctuation">(</span><span class="token punctuation">&#123;</span>
    addshard:<span class="token string">"jxk3/x.x.x.33:28019,x.x.x.37:28019,x.x.x.41:28019,"</span>,
    name: <span class="token string">"shard3"</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="7-5-测试是否搭建成功"><a href="#7-5-测试是否搭建成功" class="headerlink" title="7.5 测试是否搭建成功"></a>7.5 测试是否搭建成功</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">db.runCommand<span class="token punctuation">(</span> <span class="token punctuation">&#123;</span>enablesharding:<span class="token string">"dbtest"</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span>
db.runCommand<span class="token punctuation">(</span> <span class="token punctuation">&#123;</span>shardcollection:<span class="token string">"dbtest.one"</span>, key:<span class="token punctuation">&#123;</span>id:1<span class="token punctuation">&#125;</span> <span class="token punctuation">&#125;</span><span class="token punctuation">)</span>
use dbtest
var tmp <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
for<span class="token punctuation">(</span>var i <span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span><span class="token number">10000000</span><span class="token punctuation">;</span> i++<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    tmp.push<span class="token punctuation">(</span><span class="token punctuation">&#123;</span> <span class="token string">'id'</span>:i, <span class="token string">"name"</span><span class="token builtin class-name">:</span><span class="token string">"lunck dog "</span> + i<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
db.one.insertMany<span class="token punctuation">(</span>tmp<span class="token punctuation">)</span><span class="token punctuation">;</span>
sh.status<span class="token punctuation">(</span><span class="token punctuation">)</span>


<span class="token comment">###进入数据库</span>
db.one.count<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="八-常用命令汇总"><a href="#八-常用命令汇总" class="headerlink" title="八 常用命令汇总"></a>八 常用命令汇总</h2><p>1.分片命令</p>
<blockquote>
<p>mongod -f /opt/mongodb4.2/conf/mongodb1.conf 启动<br>mongod -f /opt/mongodb4.2/conf/mongodb1.conf –shutdown 关闭<br>OR<br>mongo -port 27017<br>use admin<br>db.shutdownServers()</p>
</blockquote>
<p>2.config 复制集命令</p>
<blockquote>
<p>mongod -f /opt/mongodb4.2/conf/mongodb-cfg.conf 启动<br>mongod -f /opt/mongodb4.2/conf/mongodb-cfg.conf –shutdown 关闭</p>
</blockquote>
<p>3.mongos命令</p>
<blockquote>
<p>mongos -f /opt/mongodb4.2/conf/mongos.conf 启动<br>mongo -port 27017 关闭<br>use admin<br>db.shutdownServers()</p>
</blockquote>
<p>4.增加副本和复制点<br>use admin<br>db.auth(‘username’,’password’)<br>rs.add(‘x.x.x.33:28017’);<br>rs.add(‘x.x.x.33:27017’)</p>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/about" rel="external nofollow noreferrer">Jack</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://jack006.com/2022/01/15/mongo-shard/">https://jack006.com/2022/01/15/mongo-shard/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/about" target="_blank">Jack</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/mongo/">
                                    <span class="chip bg-color">mongo</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.png" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.jpg" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    
        <style>
    .valine-card {
        margin: 1.5rem auto;
    }

    .valine-card .card-content {
        padding: 20px 20px 5px 20px;
    }

    #vcomments textarea {
        box-sizing: border-box;
        background: url("/medias/comment_bg.png") 100% 100% no-repeat;
    }

    #vcomments p {
        margin: 2px 2px 10px;
        font-size: 1.05rem;
        line-height: 1.78rem;
    }

    #vcomments blockquote p {
        text-indent: 0.2rem;
    }

    #vcomments a {
        padding: 0 2px;
        color: #4cbf30;
        font-weight: 500;
        text-decoration: none;
    }

    #vcomments img {
        max-width: 100%;
        height: auto;
        cursor: pointer;
    }

    #vcomments ol li {
        list-style-type: decimal;
    }

    #vcomments ol,
    ul {
        display: block;
        padding-left: 2em;
        word-spacing: 0.05rem;
    }

    #vcomments ul li,
    ol li {
        display: list-item;
        line-height: 1.8rem;
        font-size: 1rem;
    }

    #vcomments ul li {
        list-style-type: disc;
    }

    #vcomments ul ul li {
        list-style-type: circle;
    }

    #vcomments table, th, td {
        padding: 12px 13px;
        border: 1px solid #dfe2e5;
    }

    #vcomments table, th, td {
        border: 0;
    }

    table tr:nth-child(2n), thead {
        background-color: #fafafa;
    }

    #vcomments table th {
        background-color: #f2f2f2;
        min-width: 80px;
    }

    #vcomments table td {
        min-width: 80px;
    }

    #vcomments h1 {
        font-size: 1.85rem;
        font-weight: bold;
        line-height: 2.2rem;
    }

    #vcomments h2 {
        font-size: 1.65rem;
        font-weight: bold;
        line-height: 1.9rem;
    }

    #vcomments h3 {
        font-size: 1.45rem;
        font-weight: bold;
        line-height: 1.7rem;
    }

    #vcomments h4 {
        font-size: 1.25rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    #vcomments h5 {
        font-size: 1.1rem;
        font-weight: bold;
        line-height: 1.4rem;
    }

    #vcomments h6 {
        font-size: 1rem;
        line-height: 1.3rem;
    }

    #vcomments p {
        font-size: 1rem;
        line-height: 1.5rem;
    }

    #vcomments hr {
        margin: 12px 0;
        border: 0;
        border-top: 1px solid #ccc;
    }

    #vcomments blockquote {
        margin: 15px 0;
        border-left: 5px solid #42b983;
        padding: 1rem 0.8rem 0.3rem 0.8rem;
        color: #666;
        background-color: rgba(66, 185, 131, .1);
    }

    #vcomments pre {
        font-family: monospace, monospace;
        padding: 1.2em;
        margin: .5em 0;
        background: #272822;
        overflow: auto;
        border-radius: 0.3em;
        tab-size: 4;
    }

    #vcomments code {
        font-family: monospace, monospace;
        padding: 1px 3px;
        font-size: 0.92rem;
        color: #e96900;
        background-color: #f8f8f8;
        border-radius: 2px;
    }

    #vcomments pre code {
        font-family: monospace, monospace;
        padding: 0;
        color: #e8eaf6;
        background-color: #272822;
    }

    #vcomments pre[class*="language-"] {
        padding: 1.2em;
        margin: .5em 0;
    }

    #vcomments code[class*="language-"],
    pre[class*="language-"] {
        color: #e8eaf6;
    }

    #vcomments [type="checkbox"]:not(:checked), [type="checkbox"]:checked {
        position: inherit;
        margin-left: -1.3rem;
        margin-right: 0.4rem;
        margin-top: -1px;
        vertical-align: middle;
        left: unset;
        visibility: visible;
    }

    #vcomments b,
    strong {
        font-weight: bold;
    }

    #vcomments dfn {
        font-style: italic;
    }

    #vcomments small {
        font-size: 85%;
    }

    #vcomments cite {
        font-style: normal;
    }

    #vcomments mark {
        background-color: #fcf8e3;
        padding: .2em;
    }

    #vcomments table, th, td {
        padding: 12px 13px;
        border: 1px solid #dfe2e5;
    }

    table tr:nth-child(2n), thead {
        background-color: #fafafa;
    }

    #vcomments table th {
        background-color: #f2f2f2;
        min-width: 80px;
    }

    #vcomments table td {
        min-width: 80px;
    }

    #vcomments [type="checkbox"]:not(:checked), [type="checkbox"]:checked {
        position: inherit;
        margin-left: -1.3rem;
        margin-right: 0.4rem;
        margin-top: -1px;
        vertical-align: middle;
        left: unset;
        visibility: visible;
    }
</style>

<div class="card valine-card" data-aos="fade-up">
    <div class="comment_headling" style="font-size: 20px; font-weight: 700; position: relative; padding-left: 20px; top: 15px; padding-bottom: 5px;">
        <i class="fas fa-comments fa-fw" aria-hidden="true"></i>
        <span>评论</span>
    </div>
    <div id="vcomments" class="card-content" style="display: grid">
    </div>
</div>

<script src="/libs/valine/av-min.js"></script>
<script src="/libs/valine/Valine.min.js"></script>
<script>
    new Valine({
        el: '#vcomments',
        appId: 'heVROg7OJ1PBHndbrewY0tXp-gzGzoHsz',
        appKey: '94TnziBTBosfS9O1cGrGcvRt',
        notify: 'false' === 'true',
        verify: 'false' === 'true',
        visitor: 'true' === 'true',
        avatar: 'mm',
        pageSize: '10',
        lang: 'zh-cn',
        placeholder: 'just go go'
    });
</script>

<!--酷Q推送-->


    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2022/01/17/bilibili/">
                    <div class="card-image">
                        
                        <img src="/medias/postimages/bilibili.png" class="responsive-img" alt="bilibili视频下载">
                        
                        <span class="card-title">bilibili视频下载</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            下载一波 bilibili 视频
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2022-01-17
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/%E6%8A%98%E8%85%BE/" class="post-category">
                                    折腾
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/%E5%B7%A5%E5%85%B7/">
                        <span class="chip bg-color">工具</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2022/01/15/pmp-dialog/">
                    <div class="card-image">
                        
                        <img src="/medias/postimages/pmp-dialog.jpg" class="responsive-img" alt="与PMP对话">
                        
                        <span class="card-title">与PMP对话</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            问一问 PMP
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2022-01-15
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/pmp/" class="post-category">
                                    pmp
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/pmp/">
                        <span class="chip bg-color">pmp</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4, h5'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4, h5').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        
        display: none;
        
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        
        display: none;
        
        font-size: 15px;
        color: #42b983;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="playlist"
                   id="647710478"
                   fixed='true'
                   autoplay='false'
                   theme='#42b983'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/libs/aplayer/APlayer.min.js"></script>
<script src="/libs/aplayer/Meting.min.js"></script>

    

    <div class="container row center-align"
         style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2021-2022</span>
            
            <a href="/about" target="_blank">jack006</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            
            
                
            
            
                <span id="busuanzi_container_site_pv">
                &nbsp;|&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;
                    <span id="busuanzi_value_site_pv" class="white-color"></span>
            </span>
            
            
                <span id="busuanzi_container_site_uv">
                &nbsp;|&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;
                    <span id="busuanzi_value_site_uv" class="white-color"></span>
            </span>
            
            <br>

            <!-- 运行天数提醒. -->
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/mybasket-jack" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:2354738434@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=2354738434" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 2354738434" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







    <a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fas fa-rss"></i>
    </a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    

    

    <!-- 雪花特效 -->
    

    <!-- 鼠标星星特效 -->
    

     
        <script src="https://ssl.captcha.qq.com/TCaptcha.js"></script>
        <script src="/libs/others/TencentCaptcha.js"></script>
        <button id="TencentCaptcha" data-appid="xxxxxxxxxx" data-cbfn="callback" type="button" hidden></button>
    

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    <!--腾讯兔小巢-->
    
    

    

    

    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
